Imports System.Xml
Imports System.Data.SqlClient
Public Class dMarca
    Private Function DBConnect() As SqlConnection
        Dim dts_XML As DataSet = New DataSet()
        dts_XML.ReadXml("C:\Tesis\TGB\TGB\Connexion.xml")
        Return New SqlConnection(dts_XML.Tables("Connection").Rows(0).Item("ConnectionString"))
    End Function
    Private Function Consultar(ByVal NombreStore As String, ByVal Parametros As ArrayList) As DataSet
        Dim cnn As SqlConnection = DBConnect()
        cnn.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = cnn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = NombreStore
        cmd.Parameters.Clear()
        Dim i As Integer = 0
        For i = 0 To Parametros.Count - 1
            cmd.Parameters.Add(Parametros(i))
        Next
        Dim dta As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dts As DataSet = New DataSet
        dta.Fill(dts)
        cnn.Close()
        Return dts
    End Function
    Private Function ABM(ByVal Consulta As String, ByVal Parametros As ArrayList) As Boolean
        Dim okas As Boolean = False
        Dim cnn As SqlConnection = DBConnect()
        cnn.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = cnn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = Consulta
        cmd.Parameters.Clear()
        Dim i As Integer = 0
        For i = 0 To Parametros.Count - 1
            cmd.Parameters.Add(Parametros(i))
        Next
        okas = cmd.ExecuteNonQuery() <> 0
        cnn.Close()
        Return okas
    End Function
    Public Function Existe(ByVal CodigoMarca As String) As Boolean
        Dim Parametros As ArrayList = New ArrayList
        Parametros.Add(New SqlParameter("@pECodigoMarca", CodigoMarca))
        Return (Consultar("STORE_TblMarcaBuscar", Parametros).Tables(0).Rows(0).ItemArray(0) = 1)
    End Function
    Public Function Insertar(ByVal Descripcion As String) As Boolean
        Dim Parametros As ArrayList = New ArrayList
        Parametros.Add(New SqlParameter("@pEDescripcion", Descripcion))
        Return ABM("STORE_TblMarcaAlta", Parametros)
    End Function
    Public Function Eliminar(ByVal CodigoMarca As String) As Boolean
        Dim Parametros As ArrayList = New ArrayList
        Parametros.Add(New SqlParameter("@pECodigoMarca", CodigoMarca))
        Return ABM("STORE_TblMarcaELIMINAR", Parametros)
    End Function
    Public Function Modificar(ByVal CodigoMarca As String, ByVal Descripcion As String) As Boolean
        Dim Parametros As ArrayList = New ArrayList
        If CodigoMarca <> "" Then
            Parametros.Add(New SqlParameter("@pECodigoMarca", CodigoMarca))
            If Descripcion <> "" Then
                Parametros.Add(New SqlParameter("@pEDescripcion", Descripcion))
            End If
        End If
        Return ABM("STORE_TblMarcaMODIFICAR", Parametros)
    End Function
    Public Function BuscarFiltrado(ByVal CodigoMarca As String, ByVal Descripcion As String) As DataSet
        Dim Parametros As ArrayList = New ArrayList
        If CodigoMarca <> "" Then
            Parametros.Add(New SqlParameter("@pECodigoMarca", CodigoMarca))
        End If

        If Descripcion <> "" Then
            Parametros.Add(New SqlParameter("@pEDescripcion", Descripcion))
        End If
        Return Consultar("STORE_TblMarcaBuscar", Parametros)
    End Function
    Public Function Listar() As DataSet
        Return Consultar("STORE_TblMarcaBuscar", New ArrayList)
    End Function
    End Class
